package com.video.web.task;

import com.video.video.domain.DClassify;
import com.video.video.domain.DGoods;
import com.video.video.domain.vo.GoodsQuery;
import com.video.video.service.IDClassifyService;
import com.video.video.service.IDGoodsService;
import com.video.video.service.IDSoldGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 定时任务调度
 *
 * 将已经售卖超过1个月的商品 转移到指定分类中
 *
 */
@Component("goodsTask")
public class GoodsTask
{

    @Autowired
    private IDGoodsService idGoodsService;

    @Autowired
    private IDClassifyService idClassifyService;

    @Autowired
    private IDSoldGoodsService idSoldGoodsService;

    public void execGoodsTransfer()
    {
        try {
            List<DGoods> listGoods = idGoodsService.selectDGoodsSoldList();
            if(listGoods != null && listGoods.size()>0){
                //转移到新表中
                int result = idSoldGoodsService.batchInsertSoldDGoods(listGoods);

                DClassify dClassify = idClassifyService.selectDClassifyByName("已售出",1);
                if(dClassify != null && result > 0 ){
                    List<String> goodsIds = listGoods.stream().map(DGoods::getId).collect(Collectors.toList());
                    idGoodsService.updateCidByIds(goodsIds,dClassify.getId());
                }
//
//            if(result > 0 ){
//                String[] goodsIds = listGoods.stream().map(DGoods::getId).toArray(String[]::new);
//                if(goodsIds.length > 0){
//                    idGoodsService.deleteDGoodsByIds(goodsIds);
//                }
//            }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
